package com.test.multithread.classLoader;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class ClassLoaderDemo {

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        ClassLoader myLoader = new ClassLoader() {
            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {
                try {
                    String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
                    InputStream is = getClass().getResourceAsStream(fileName);
                    if(is == null) {
                        return super.loadClass(name);
                    }
                    byte[] b = new byte[is.available()];
                    is.read(b);
                    return defineClass(name, b, 0, b.length);
                } catch(IOException e) {
                    throw new ClassNotFoundException(name);
                }
            }
        };

        Object obj = myLoader.loadClass("com.test.multithread.classLoader.ClassLoaderDemo").newInstance();

        System.out.println(obj.getClass());
        System.out.println(obj.getClass().getClassLoader());
        System.out.println(obj instanceof ClassLoaderDemo);

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>(2);
        objectObjectHashMap.put("key", "value");
    }
}

// 输出结果
/**
class com.zero.demo.ClassLoaderDemo
false
**/